"use strict";
/**
 * @name simple
 * 快速购买
 */

/**
 * @name /
 * @param string name 购买类型
 * 快速购买
 */
module.exports = async function () {
    let name = this.get('name', 'string');
    if (!name) {
        return this.error("args error");
    }
    if (name == 'hook') {
        this.updater.role.key("onhook", "stage", 'gs', 'hangPoint');
        this.updater.daily.key("hookSNum");
        return this.updater.data().then(() => {
            let speedBuyNum = this.updater.daily.get('hookSNum') || 0;
            //默认次数
            let next = speedBuyNum + 1;
            // let speedBuyConfig = this.config("hookBase", next);
            // if (!speedBuyConfig) {
            //     return this.quick_error("m2sgrbs10", "加速次数不足", speedBuyNum, next);
            // }
            const hookCostBaseConfig = this.config("dailyGrowthCost", speedBuyNum + 1);
            if (!hookCostBaseConfig) {
                return this.quick_error("m2sgrbs1", "配置不存在", "hookCost", speedBuyNum);
            }
            const hookCostConfig = hookCostBaseConfig.hook;
            if (!hookCostConfig) {
                return this.quick_error("m2sgrbs1", "配置不存在", "hookCost", speedBuyNum);
            }
            if (hookCostConfig.itemID == 0) {
                return this.quick_error("m2sgrbs1", "配置不存在", "hookCost", speedBuyNum);
            }
            console.log("测试扣件物品:", hookCostConfig.itemID, hookCostConfig.cost);
            this.updater.item.sub(hookCostConfig.itemID, hookCostConfig.cost);
            //给2个小时的奖励
            let hangPoint = this.updater.role.get("hangPoint") || 0;
            let rewardConfig = this.config("hookReward", hangPoint);
            if (!rewardConfig) {
                return this.quick_error("m2sgrbs1", "配置不存在", "hookReward", hangPoint);
            }
            let _total_add = {};
            let _hook_add = {};
            for (let i = 0; i < rewardConfig.hookGroup.length; i++) {
                let numB = 0;
                let tt = 2 * 60 * 60 * 1000;
                numB = Math.floor(rewardConfig["hookGroup"][i].num * tt / (rewardConfig["hookGroup"][i].time * 1000));
                if (numB <= 0) {
                    continue;
                }
                if (parseInt(rewardConfig["hookGroup"][i].itemID / 1000000) == 70) {
                    _hook_add[rewardConfig["hookGroup"][i].itemID] = numB;
                } else {
                    _total_add[rewardConfig["hookGroup"][i].itemID] = numB;
                }
            }
            for (let i in _hook_add) {
                let id = parseInt(i);
                if (id) {
                    for (let j = 0; j < _hook_add[id]; ++j) {
                        let { err, ret } = this.library("item/group", id, 1);
                        if (err) {
                            return this.error(err, ret);
                        }
                        if (Array.isArray(ret)) {
                            for (let v in ret) {
                                if (_total_add.hasOwnProperty(ret[v].id)) {
                                    _total_add[ret[v].id] += ret[v].num;
                                } else {
                                    _total_add[ret[v].id] = ret[v].num;
                                }
                            }
                        } else {
                            if (_total_add.hasOwnProperty(ret.id)) {
                                _total_add[ret.id] += ret.num;
                            } else {
                                _total_add[ret.id] = ret.num;
                            }
                        }

                    }
                }
            }
            for (let i in _total_add) {
                this.updater.add(parseInt(i), _total_add[i]);
            }
            this.updater.daily.set("hookSNum", next);
            return;
        }).then(() => {
            return this.updater.save();
        })
    }
    this.updater.daily.key(name);
    await this.updater.data();
    let timeNum = this.updater.daily.get(name) || 0;
    let next = timeNum + 1;
    const hookCostBaseConfig = this.config("dailyGrowthCost", next);
    if (!hookCostBaseConfig) {
        return this.quick_error("m2sgrbs1", "配置不存在", "hookCost", next);
    }
    const hookCostConfig = hookCostBaseConfig[name];
    if (!hookCostConfig) {
        return this.quick_error("m2sgrbs1", "配置不存在", "hookCost", next);
    }
    if (hookCostConfig.itemID == 0) {
        return this.quick_error("m2sgrbs1", "配置不存在", "hookCost", next);
    }
    this.updater.item.sub(hookCostConfig.itemID, hookCostConfig.cost);
    this.updater.item.add(hookCostConfig.addItem, hookCostConfig.addNum);
    this.updater.daily.add(name, 1);
    this.updater.daily.add("record.43", 1);
    this.updater.record.add(43, 1);
    return this.updater.save();
}